package com.tza.web.t_net.server.screenservices;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;

import com.tza.web.t_net.shared.entity.DataMap;

public class GenericDataProvider implements BeanWithId
{
   public static final String BEANID = "GenericDataProvider";

   public String getBeanId()
   {

      return BEANID;
   }

   @Autowired(required = false)
   private List<FormService>        formServices;

   private Map<String, FormService> formServiceMap;

   @PostConstruct
   public void manageMap()
   {

      formServiceMap = new HashMap<String, FormService>();

      if (formServices != null)
      {
         for (FormService formService : formServices)
         {
            if (!formServiceMap.containsKey(formService.getBeanId()))
            {
               formServiceMap.put(formService.getBeanId(), formService);
            }
         }
      }

   }

   public DataMap getData(DataMap inParameters, String beanId)
   {

      long start = System.currentTimeMillis();
      try
      {

         FormService blockService = null;

         if (!formServiceMap.containsKey(beanId))
         {

            return null;
         }
         blockService = formServiceMap.get(beanId);

         try
         {
            DataMap result = blockService.getData(inParameters);

            long elapsedTime = System.currentTimeMillis() - start;

            return result;
         }
         catch (Exception e)
         {

            return null;
         }
      }
      catch (Exception e)
      {

         return null;
      }
   }

}
